package org.apache.shiro.demo;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author fangtongle
 * @version 1.0
 * @description 自定义Realm
 * @date 2018/4/20
 */
public class CustomRealm extends AuthorizingRealm {

    // 模拟数据库
    Map<String, String> userMap = new HashMap<>();

    {
        // 加密后的密码
        userMap.put("fangtongle", "c4fb740e39b4c1dd494665cc11c45501");
        super.setName("CustomRealm");
    }

    /**
     * 授权校验(角色以及权限)
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) principalCollection.getPrimaryPrincipal();
        // 模拟从数据库或者缓存(User -> Role)
        Set<String> roles = getRoleByUserName(userName);

        // 模拟从数据库或者缓存(User -> Permission)
        Set<String> permissions = getPermissionByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);

        return simpleAuthorizationInfo;
    }


    /**
     * 认证检验
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        // 1、从主体传过来的认证信息中获得用户名
        String userName = (String) authenticationToken.getPrincipal();

        // 2、通过用户名到数据库中获取凭证(password)
        String password = getPasswordByUserName(userName);
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("fangtongle", password, "CustomRealm");

        // MD5加密
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("fangtongle"));

        return authenticationInfo;
    }

    /**
     * 模拟数据库
     *
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }


    /**
     * UserName -> Roles
     *
     * @param userName
     * @return
     */
    private Set<String> getRoleByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("admin");
        sets.add("user");
        return sets;
    }


    /**
     * UserName -> Permission
     *
     * @param userName
     * @return
     */
    private Set<String> getPermissionByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("user:delete");
        sets.add("user:update");
        return sets;
    }
}
